﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using EHTWebsiteProject.Code.BLL;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.Utility;
using EHTWebsite.Code;
using System.Web.Security;
using System.Web.Profile;

namespace EHTWebsiteProject
{
    public partial class CustomerOrderFinish : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Receive data for Region
                DataSet dsRegion = RegionProvider.GetRegions();
                this.ddlReceivedRegion.DataValueField = "RegionID";
                this.ddlReceivedRegion.DataTextField = "RegionName";
                this.ddlReceivedRegion.DataSource = dsRegion;
                this.ddlReceivedRegion.DataBind();
            }
        }

        protected void ddlReceivedRegion_DataBound(object sender, System.EventArgs e)
        {
            this.ddlReceivedRegion.Items.Insert(0, new ListItem("Select region..", "-1"));
        }

        private Boolean ValidateDateTime()
        {
            string msg = "";
            Boolean isSuccess = true;
            if (ddlReceivedRegion.SelectedValue == "-1")
            {
                msg = "Select a Region";
                isSuccess = false;
            }
            DateTime RequredDate;
            DateTime NowDate = new DateTime();
            RequredDate = cdRequredDate1.SelectedDate.Value;
            if (RequredDate == NowDate)
            {
                rDate.IsValid = false;
                isSuccess = false;
            }
            if (RequredDate.Date < DateTime.Now.Date)
            {
                rDate.ErrorMessage = "Required date must be equal or greater than current date.";
                rDate.IsValid = false;
                isSuccess = false;
            }
            if (!isSuccess)
            {
                ltrMessage.Text = msg;
            }
            return isSuccess;
        }

        protected void btnSave_Click(object sender, System.EventArgs e)
        {
            if (!ValidateDateTime())
                return;
            Page.Validate();
            if (Page.IsValid)
            {
                MembershipUser theUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                Order theOrder = new Order();
                theOrder.BillPictureUrl = "";
                theOrder.CustomerID = theUser.ProviderUserKey.ToString();
                theOrder.OrderID = -1;
                theOrder.OrderState = HangSo.ORDER_STATE_APPENDING;
                theOrder.ReceicedPhone = this.txtReceicedPhone.Text.Trim();
                theOrder.ReceivedAddress = this.txtReceivedAddress.Text.Trim();
                theOrder.ReceivedCity = this.txtReceivedCity.Text.Trim();
                theOrder.ReceivedName = this.txtReceivedName.Text.Trim();
                theOrder.ReceivedPostalCode = this.txtPostCode.Text.Trim();
                theOrder.ReceivedRegionID = Int32.Parse(this.ddlReceivedRegion.SelectedValue.ToString());
                theOrder.RequireDate = cdRequredDate1.SelectedDate.Value.AddHours(DateTime.Now.Hour + 5);
                theOrder.TotalAmount = ShoppingCart.Instance.GetSubTotal();
                theOrder.ServiceCost = RegionProvider.GetRegionByRegionID(Int32.Parse(this.ddlReceivedRegion.SelectedValue.ToString())).ServiceCost;
                // cập nhật vào DB
                ShoppingCart.Instance.theOrder = theOrder;
                ShoppingCart.Instance.SaveDataBase();
                // xóa thông tin ShoppingCart
                HttpContext.Current.Session["ASPNETShoppingCart"] = null; ;
                Response.Redirect("ShippingFinish.html");
            }
        }

        protected void btnBack_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("ViewCart.aspx");
        }
    }

}